package com.wanpinghui.supplier.user.company;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.user.company.adapter.BussinessCertificateAdapter;
import com.wanpinghui.supplier.user.company.adapter.ProjectCaseAdapter;
import com.wanpinghui.supplier.widgets.ExpandableTextView;
import com.wanpinghui.supplier.widgets.GoodFormatView;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.user.UserInfoService;
import com.wanpinghui.wphlibrary.model.user.bean.CaseBean;
import com.wanpinghui.wphlibrary.model.user.bean.CompanyBean;
import com.wanpinghui.wphlibrary.model.user.bean.CourseBean;
import com.wanpinghui.wphlibrary.model.user.bean.SkillBean;
import com.wanpinghui.wphlibrary.photovideo.bean.BaseUrlBean;
import com.yunshl.yunshllibrary.utils.NumberUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.utils.TimeUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit.callback.YRequestCallback;

/**
 * Created by tg596 on 2018/1/23.
 */
@ContentView(R.layout.layout_activity_companyinfo_edit)
public class CompanyInfoEditActivity extends BaseActivity {

    private TitlePanelLayoutUser mTplTitle;

    private LinearLayout mLinearLayout;
    private RelativeLayout mLlEditComInfo;
    private ScrollView scrollView;
    private TextView mIvCompanyLogo;
    /**
     * 有限公司
     */
    private TextView mTvCompanyName;
    private ImageView mIvActive;
    private RelativeLayout rl_company_header;
    /**
     * 万屏汇认证
     */
    private TextView mTvPass;
    private ExpandableTextView mEtvCompanyInfo;
    private RecyclerView recv_project_case;
    private RecyclerView recv_bussiness_certificate;

    private ProjectCaseAdapter projectCaseAdapter;

    private BussinessCertificateAdapter bussinessCertificateAdapter;
    /**
     * 编辑公司信息
     */
    private TextView mTvCompanyStatus;
    private ImageView mIvArrows;
    /**
     * 2
     */
    private TextView mTvMeeting;
    private LinearLayout mLlDevelopmentpath;
    /**
     * 企业类型：有限责任公司(自然人投资或控股)\n注册资本：2010万元\n注册时间：2011-01-25
     */
    private TextView mTvCompanyInfo;
    private LinearLayout mLlCompanyInfo;

    private LinearLayout ll_case;

    private RelativeLayout ll_certificates;

    private LinearLayout ll_course;

    private LinearLayout ll_skill;

    @ViewInject(R.id.gfv_comment_reason)
    private GoodFormatView gfv_comment_reason;

    private CompanyBean companyBean;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        mTplTitle = (TitlePanelLayoutUser) findViewById(R.id.tpl_title);
        mTvCompanyStatus = (TextView) findViewById(R.id.tv_company_status);
        mIvArrows = (ImageView) findViewById(R.id.iv_arrows);
        scrollView = (ScrollView) findViewById(R.id.scrollView);
        mLlEditComInfo = (RelativeLayout) findViewById(R.id.ll_edit_com_info);
        mIvCompanyLogo = (TextView) findViewById(R.id.iv_company_logo);
        mTvCompanyName = (TextView) findViewById(R.id.tv_company_name);
        mIvActive = (ImageView) findViewById(R.id.iv_active);
        mTvPass = (TextView) findViewById(R.id.tv_pass);
        mTvMeeting = (TextView) findViewById(R.id.tv_meeting);
        mEtvCompanyInfo = (ExpandableTextView) findViewById(R.id.etv_company_info);
        mLlDevelopmentpath = (LinearLayout) findViewById(R.id.ll_developmentpath);
        mTvCompanyInfo = (TextView) findViewById(R.id.tv_company_info);
        mLlCompanyInfo = (LinearLayout) findViewById(R.id.ll_company_info);
        recv_project_case = (RecyclerView) findViewById(R.id.recyclerview);
        recv_bussiness_certificate = (RecyclerView) findViewById(R.id.recv_bussiness_certificate);
        rl_company_header = (RelativeLayout)findViewById(R.id.rl_company_header);
        ll_case = (LinearLayout)findViewById(R.id.ll_case);
        ll_certificates = (RelativeLayout)findViewById(R.id.ll_certificates);
        ll_course =(LinearLayout)findViewById(R.id.ll_course);
        ll_skill = (LinearLayout)findViewById(R.id.ll_skill);
    }

    @Override
    public void bindEvents() {
        mLlEditComInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CompanyInfoEditActivity.this, CompanyNameActivity.class);
                intent.putExtra("edit", true);
                if (companyBean != null){
                    intent.putExtra("id",companyBean.getId());
                }
                startActivity(intent);
            }
        });
        mTplTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void initData() {
        companyBean = getIntent().getParcelableExtra("bean");
        if (companyBean != null){
            showData(companyBean);
        }else if (getIntent().getBooleanExtra("editFinish",false)){
            WPHSdk.getService(UserInfoService.class)
                    .getMyCompany(new YRequestCallback<CompanyBean>() {

                        @Override
                        public void onSuccess(CompanyBean var1) {
                            if (var1 != null) {
                                showData(var1);

                            }
                        }

                        @Override
                        public void onFailed(int var1, String message) {
                            showWarningText(message);
                        }

                        @Override
                        public void onException(Throwable var1) {

                        }
                    });

        }



//        for (int i = 0; i< 4;i++){
//            mLlDevelopmentpath.addView(createView());
//        }


    }

    private void showData(CompanyBean companyBean) {
        if (companyBean.getAudit_status() == 1){  // 1待提交/11审理中/21审理失败/51审理结束
            if (companyBean.getId() >0) {
                if (TextUtil.isNotEmpty(companyBean.getLogo())) {
                    mIvCompanyLogo.setText("");
                    Glide.with(getApplicationContext()).load(companyBean.getLogo()+"_thumbnail").error(R.mipmap.loading_140x140).thumbnail(0.5f).into(new SimpleTarget<GlideDrawable>() {
                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                            mIvCompanyLogo.setBackground(resource);
                        }

                    });
                }
                mTvMeeting.setText(String.valueOf(companyBean.getView_times()));
                mTvCompanyName.setText(companyBean.getCompany_name());

                showInfo(companyBean);
            }
    //        scrollView.setVisibility(View.INVISIBLE);
        }else if (companyBean.getAudit_status() == 11 ) {
            mLlEditComInfo.setBackgroundColor(ContextCompat.getColor(this, R.color.colorYellow_ffecce));
            mTvCompanyStatus.setTextColor(ContextCompat.getColor(this, R.color.colorPrimaryYellow));
            mTvCompanyStatus.setText("审核中");
            mIvArrows.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.common_icon_arrow_yellow_1));
            if (companyBean.getId()>0) {
                scrollView.setVisibility(View.VISIBLE);
                rl_company_header.setVisibility(View.VISIBLE);
                if (TextUtil.isNotEmpty(companyBean.getLogo())) {
                    mIvCompanyLogo.setText("");
                    Glide.with(getApplicationContext()).load(companyBean.getLogo()+"_thumbnail").into(new SimpleTarget<GlideDrawable>() {
                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                            mIvCompanyLogo.setBackground(resource);
                        }
                    });
                }
                mTvMeeting.setText(String.valueOf(companyBean.getView_times()));
                mTvCompanyName.setText(companyBean.getCompany_name());
                showInfo(companyBean);
            }else {

                scrollView.setVisibility(View.INVISIBLE);
            }

        }else if (companyBean.getAudit_status() == 21 ){
            mLlEditComInfo.setBackgroundColor(ContextCompat.getColor(this, R.color.colorbgWarning_FFE2DF));
            mTvCompanyStatus.setTextColor(ContextCompat.getColor(this, R.color.colorBgWarningText));
            mTvCompanyStatus.setText("审核失败");
            mIvArrows.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.common_icon_arrow_red_1));
            if (companyBean.getId()>0) {

                scrollView.setVisibility(View.VISIBLE);
                rl_company_header.setVisibility(View.VISIBLE);
                if (TextUtil.isNotEmpty(companyBean.getLogo())) {
                    mIvCompanyLogo.setText("");
                    Glide.with(getApplicationContext()).load(companyBean.getLogo()+"_thumbnail").into(new SimpleTarget<GlideDrawable>() {
                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                            mIvCompanyLogo.setBackground(resource);
                        }
                    });
                }
                mTvMeeting.setText(String.valueOf(companyBean.getView_times()));
                mTvCompanyName.setText(companyBean.getCompany_name());

                showInfo(companyBean);

            }else {

                scrollView.setVisibility(View.INVISIBLE);
            }
        }else if (companyBean.getAudit_status() == 51 || companyBean.getAudit_status() == 0 ){
            if (TextUtil.isNotEmpty(companyBean.getLogo())){
                mIvCompanyLogo.setText("");
                Glide.with(getApplicationContext()).load(companyBean.getLogo()+"_thumbnail").error(R.mipmap.loading_140x140).thumbnail(0.5f).into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        mIvCompanyLogo.setBackground(resource);
                    }

                });
            }
            mTvMeeting.setText(String.valueOf(companyBean.getView_times()));
            mTvCompanyName.setText(companyBean.getCompany_name());

            showInfo(companyBean);


        }
    }

    private void showInfo(CompanyBean companyBean) {
        mEtvCompanyInfo.setText(companyBean.getIntro());
        List<CaseBean> caseBeanList = companyBean.getCaseList();
        if (caseBeanList != null && caseBeanList.size()>0){
            projectCaseAdapter = new ProjectCaseAdapter(this);
            recv_project_case.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
            recv_project_case.setNestedScrollingEnabled(false);
            recv_project_case.setAdapter(projectCaseAdapter);
            projectCaseAdapter.setDatas(companyBean.getCaseList());
        }else {
           ll_case.setVisibility(View.GONE);
        }
        if (TextUtil.isNotEmpty(companyBean.getCertificates())) {
            List<BaseUrlBean> baseUrlBeanList = new Gson().fromJson(companyBean.getCertificates().length() < 3 ? null : companyBean.getCertificates(), new TypeToken<List<BaseUrlBean>>() {
            }.getType());
            if (baseUrlBeanList != null && baseUrlBeanList.size() > 0) {
                bussinessCertificateAdapter = new BussinessCertificateAdapter(this);
                recv_bussiness_certificate.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
                recv_bussiness_certificate.setNestedScrollingEnabled(false);
                recv_bussiness_certificate.setAdapter(bussinessCertificateAdapter);
                bussinessCertificateAdapter.setDatas(baseUrlBeanList);
            } else {
                ll_certificates.setVisibility(View.GONE);
            }
        }else {
            ll_certificates.setVisibility(View.GONE);
        }
        List<CourseBean> courseBeanList = companyBean.getCourseList();
        if (courseBeanList != null && courseBeanList.size() > 0 ){
            mLlDevelopmentpath.removeAllViews();
            for (CourseBean bean : courseBeanList){
                mLlDevelopmentpath.addView(createView(bean));     ;
            }
        }else {
            ll_course.setVisibility(View.GONE);
        }
        if (companyBean.getRegistered_capital()>100000) {
            int Registered_capital = (int) companyBean.getRegistered_capital() / 10000;
            mTvCompanyInfo.setText("企业类型：" + companyBean.getCompany_type()
                    + "\n注册资本：" + Registered_capital + "万元"
                    + "\n注册时间：" + TimeUtil.format(companyBean.getRegistered_date() * 1000, "yyyy-MM-dd"));
        }else {
            mTvCompanyInfo.setText("企业类型：" + companyBean.getCompany_type()
                    + "\n注册资本：" + NumberUtil.double2String(companyBean.getRegistered_capital())
                    + "\n注册时间：" + TimeUtil.format(companyBean.getRegistered_date() * 1000, "yyyy-MM-dd"));
        }

        List<SkillBean> skillBeanList = companyBean.getSkillList();

        if (skillBeanList != null&& skillBeanList.size()>0){
            gfv_comment_reason.setTextColor(R.color.colorPrimaryBlue);
            gfv_comment_reason.setData(skillBeanList);
        }else {
            ll_skill.setVisibility(View.GONE);
        }
    }


    private View createView(CourseBean bean){
        View view = LayoutInflater.from(this).inflate(R.layout.item_developmentpath,null);
        TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_content.setText(bean.getCourse_content());
        tv_time.setText(TimeUtil.format(bean.getCourse_date()*1000, "yyyy-MM-dd"));


        return view;
    }

}
